import bge

##
## NOTE: You generally do not have to edit/change anything here.
##
## This script handles mouse and keyboard input for general functions, such
## as selection of objects, key presses for cameras, etc. It does not have 
## anything to do with detailed persona/truck interactions. 
##


controller  = bge.logic.getCurrentController()
owner       = controller.owner
scene       = bge.logic.getCurrentScene()
width       = bge.render.getWindowWidth()
height      = bge.render.getWindowHeight()

LMB         = controller.sensors["LMB"]
mouseOver   = controller.sensors["MouseOver"]
Msg         = controller.sensors["Message"]
sensor      = controller.sensors["Keyboard"]
mousePos    = controller.sensors["MousePos"]    # Where user clicked
hitPos      = mouseOver.hitPosition             # Where user clicked
hitObj      = mouseOver.hitObject               # The object that was clicked

addGUI      = controller.actuators["addGUI"]
remGUI      = controller.actuators["remGUI"]
addGUIinfo  = controller.actuators["addGUI-info"]
remGUIinfo  = controller.actuators["remGUI-info"]
addGUIstates  = controller.actuators["addGUI-states"]
remGUIstates  = controller.actuators["remGUI-states"]



def hideHotspots(hide):
    for obj in bge.hotspots:
        scene.objects[obj.name].visible = hide



# General keyboard input handler
for key,status in sensor.events:
    if status == bge.logic.KX_INPUT_JUST_ACTIVATED:

        if key == bge.events.RKEY:
            if bge.ik == True: 
                bge.ik = False
            else:
                bge.ik = True
        if key == bge.events.F1KEY:
            scene.active_camera = bge.cameras[0]
            print (scene.cameras, scene.active_camera)    

        if key == bge.events.F2KEY:
            scene.active_camera = bge.cameras[1]
            print (scene.cameras, scene.active_camera)

        if key == bge.events.F3KEY:
            scene.active_camera = bge.cameras[2]
            print (scene.cameras, scene.active_camera)

        if key == bge.events.SPACEKEY:
            bge.suspend = True
            controller.activate(addGUIstates)

## GUI functions

# GUI main window, activated when mouse is in upper left corner    
if mousePos.position[0] < 15 and mousePos.position[1] < 15:
    if "GUI" in bge.logic.getSceneList():
        pass
    else:
        bge.suspend = True
        controller.activate(addGUI)


# GUI info window, activated when mouse is in upper right corner    
if mousePos.position[0] > (width - 15) and mousePos.position[1] < (15):
    if "GUI-info" in bge.logic.getSceneList():
        pass
    else:
        bge.suspend = True
        controller.activate(addGUIinfo)

# A message is sent back from GUI to hide it
if Msg.positive:
    if Msg.subjects[0] == "hideGUIinfo":
        controller.activate(remGUIinfo)
        bge.suspend = False        
    if Msg.subjects[0] == "hideGUI":
        controller.activate(remGUI)
        bge.suspend = False                
    if Msg.subjects[0] == "hideGUIstates":
        controller.activate(remGUIstates)
        bge.suspend = False                



## Mouse interactions

if LMB.positive and hitObj != None:
    if 'gameObject' in hitObj:
        if 'refParent' in hitObj:
            bge.selectObj = hitObj.parent
            for i in hitObj['refParent']:
                bge.selectObj = bge.selectObj.parent

        else:
            bge.selectObj = hitObj
    else:
        pass
        # This is where general object interaction can be implemented:
        # Object is selected, but it's not a game object; useful for buildings
        # and other world objects